/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2025-02-11 15:14:03
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2025-02-19 09:00:46
 * @FilePath: /vue3-admin-server/src/entities/base.entity.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import {
    CreateDateColumn,
    UpdateDateColumn,
    PrimaryGeneratedColumn,
    AfterLoad
} from 'typeorm';

export class BaseEntity {
    @CreateDateColumn({ name: 'create_time' })
    createTime: string;

    @UpdateDateColumn({ name: 'update_time' })
    updateTime: string;

    @PrimaryGeneratedColumn()
    id: number;

    // 添加方法来格式化日期
    formatDate(date: Date): string {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }

    // 添加 @AfterLoad 钩子方法
    @AfterLoad()
    handleDateFormat(): void {
        if (this.createTime) {
            this.createTime = this.formatDate(
                this.createTime as unknown as Date
            );
        }
        if (this.updateTime) {
            this.updateTime = this.formatDate(
                this.updateTime as unknown as Date
            );
        }
    }
}
